<?php

/**
 * 文章推荐位管理
 * @author  xiaobai
 * @date    2018-01-28
 */

namespace app\panel\controller;

use app\common\controller\Panelbase;
use app\common\model\Position as position_model;
use app\common\model\PositionData;

class Position extends Panelbase
{
    public function initialize()
    {
        parent::initialize();
        $this->model         = new position_model();
        $this->position_data = new PositionData();
        $this->validate_name = 'position';

        $status = $this->model->getStatus();
        $this->assign('status_arr', $status);
    }

    public function index()
    {
        $count = $this->model->count('id');
        $list  = $this->model->order('id desc')->paginate(25);

        $this->assign("list", $list);
        return $this->fetch();
    }

    // 删除推荐位和对应的内容
    public function delete()
    {
        $id = input("param.id", 0, 'intval');
        if (empty($id) || !is_numeric($id)) {
            $this->error('参数错误');
        }

        $this->model->startTrans();

        $del_res = $this->model->where(['id'=>$id])->delete();
        if ($del_res === false) {
            $this->model->rollback();
            $this->error('删除推荐位失败');
        }

        $position_del = $this->position_data->where(['position_id'=>$id])->delete();
        if ($position_del === false) {
            $this->model->rollback();
            $this->error('删除推荐位数据失败');
        }

        $this->model->commit();
        $this->success('删除成功');
    }

    /**
     * 推荐位文章列表
     * @return mixed
     */
    public function newslist()
    {
        $position_id = input('param.posid');

        // 推荐位信息
        $position_info = $this->model->where(['id'=>$position_id])->find();

        // 推荐的内容信息
        $position_where = array('position_id'=>$position_id);
        $news_count     = $this->position_data->where($position_where)->count('id');
        $news_list      = $this->position_data->where($position_where)->order('sort asc, news_id desc')->paginate(10);

        $static_data = $this->position_data->getStaticData();
        $this->assign("types", $static_data['types']);
        $this->assign("list", $news_list);
        $this->assign('info', $position_info);
        return $this->fetch();
    }

    /**
     * 移除推荐位里的文章
     */
    public function remove()
    {
        $id = input("param.id", 0, 'intval');
        if (empty($id) || !is_numeric($id)) {
            $this->error('参数错误');
        }
        $res = model('common/PositionData')->where(array('id'=>$id))->delete();
        if ($res !== false) {
            $this->success('移除成功');
        } else {
            $this->error('移除失败');
        }
    }

    public function newsOrders()
    {
        $post = input("post.");
        if (empty($post['sort']) || !is_array($post['sort'])) {
            $this->error('数据不能为空');
        }
        $data = array();
        foreach ($post['sort'] as $key=>$val) {
            $data[$key]['id'] = $key;
            $data[$key]['sort'] = $val;
        }
        $res = $this->position_data->saveAll($data);
        if ($res !== false) {
            $this->success('排序成功');
        } else {
            $this->error('排序失败');
        }
    }

}
